﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class PublishCircleActivity : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            PublishCircleActivityOutParam result = new PublishCircleActivityOutParam();
            PublishCircleActivityInParam param = new PublishCircleActivityInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            { 
                param = inParam as PublishCircleActivityInParam;
                if (string.IsNullOrEmpty(param.user_id) || string.IsNullOrEmpty(param.community_id) || string.IsNullOrEmpty(param.name) ||
                    string.IsNullOrEmpty(param.time) || string.IsNullOrEmpty(param.address) || string.IsNullOrEmpty(param.content) || 
                    string.IsNullOrEmpty(param.endtime))
                {
                    result.code = "0";
                    result.desc = "用户ID，群ID,活动名称,活动时间,结束时间,活动地点,活动内容不能为空";

                    isFlag = false;
                }


                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }
                if (isFlag)
                {
                    try
                    {
                        DateTime dtTemp = Convert.ToDateTime(param.time);
                        dtTemp = Convert.ToDateTime(param.endtime);
                    }
                    catch
                    {
                        result.code = "0";
                        result.desc = "活动时间或活动结束时间不正确";

                        isFlag = false;
                    }
                }

                if(isFlag)
                {
                    //用户是否存在
                    isFlag = InterfaceHelper.CheckMSCircleStatus(param.community_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该群不存在或者被禁用"; 
                    }
                }

                if (isFlag)
                {
                    isFlag = InterfaceHelper.CheckMSCircle(param.user_id, param.community_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "你不属于该群的成员，不能发布活动";
                    }
                }

                if (isFlag)
                {
                    isFlag = InterfaceHelper.CheckUserIsParty(param.user_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "你不是党员，不能发布活动";
                    }
                }

                if (isFlag)
                {
                    //保存数据   
                    InterfaceHelper.InsertMSCircleActivity(param.name, param.time, param.address, param.content, param.user_id, param.community_id, param.endtime);
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-PublishCircleActivity", SerializeHelper.JsonSerializer<PublishCircleActivityInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<PublishCircleActivityInParam, PublishCircleActivityOutParam>();
            else
                SafeProcessRequest<PublishCircleActivityInParam, PublishCircleActivityOutParam>();
        }
    }

    #region 请求参数
    public class PublishCircleActivityInParam
    { 
        /// <summary>
        ///	用户ID	
        /// </summary>
        public string user_id
        { get; set; } 

        /// <summary>
        /// 圈子ID	
        /// </summary>
        public string community_id
        { get; set; } 

        /// <summary>
        ///	活动名称	
        /// </summary>
        public string name
        { get; set; } 

        /// <summary>
        ///	活动时间	
        /// </summary>
        public string time
        { get; set; }

        /// <summary>
        ///	结束时间	
        /// </summary>
        public string endtime
        { get; set; } 

        /// <summary>
        ///	活动地点	
        /// </summary>
        public string address
        { get; set; } 

        /// <summary>
        ///	活动内容	
        /// </summary>
        public string content
        { get; set; } 
    }
    #endregion

    #region 结果参数
    public class PublishCircleActivityOutParam
    {
        public PublishCircleActivityOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    }  
    #endregion 
}